﻿<?php
include_once("/../initial.php");
include_once(constant("PROJECT_DIR")."/dbdriver/courseWHandle.php");
include_once(constant("PROJECT_DIR")."/dbdriver/courseRHandle.php");
class courseLogic
{
    public function insert_course($array)
    {
        if(is_array($array)==false)
        {
            return false;
        }
        if(count($array)==0)
        {
            return false;
        }
        $whandle = new courseWHandle();
        return $whandle->insert_data($array);
    }
    public function get_course($array='',$orderby="",$limit="")
    {
        $rhandle = new courseRHandle();
        if($array == '')
        {
            return $rhandle->get_all_data($orderby,$limit);
        }
        else
        {
            return $rhandle->get_some_data($array,$orderby,$limit);
        }
    }
    public function get_course_num($array='')
    {
        $rhandle = new courseRHandle();
        return $rhandle->get_course_num($array);
    }
    public function update_course($array,$id)
    {
        $handle = new courseWHandle();
        return $handle->update_course($array,$id);
    }
    public function delete_course($id)
    {
        $handle = new courseWHandle();
        return $handle->delete_course($id);
    }
    
    public function delete_course_user($cid, $uid)
    {
        $handle = new courseWHandle();
        $condition=array("uid"=>$uid, "cid"=>$cid);
        return $handle->delete_course_user($condition);
    }
    public function delete_course_device($cid, $did)
    {
        $handle = new courseWHandle();
        $condition=array("cid"=>$cid, "did"=>$did);
        return $handle->delete_course_device($condition);
    }
    public function get_course_users($cid)
    {
        $rhandle = new courseRHandle();
        $array=array("cid"=>$cid);
        return $rhandle->get_course_users($array, 'order by createtime desc');
    }
    public function get_course_devices($cid)
    {
        $rhandle = new courseRHandle();
        $array=array("cid"=>$cid);
        return $rhandle->get_course_devices($array, 'order by createtime desc');
    }
    public function get_booktime_by_user_course($uid, $cid)
    {
    		$rhandle = new userRhandle();
    		$array=$rhandle->get_booktime_by_user_course($uid, $cid);
    		return $array[0]['booktime'];
    }
    
    public function searchCourse($keyword, $teacherid='')
    {
    		if(strlen($keyword)<3)
    		{
    				return array();
    		}
    		$cr = new courseRHandle();
    		if($teacherid==''){
	    		return $cr->searchCourse($keyword, "limit 0, 30");
	    	}
	    	return $cr->searchCourse($keyword, $teacherid, "limit 0, 30");
    }
    
}
?>
